package org.example.hello;

import com.eric.ipc.aidl.IMediaPlayer;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class HelloAndroidActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
	   private Button mPlayBtn;
	   private Button mStopBtn;
	   private Button mExitBtn;
	   private TextView mShowText;
	   private IMediaPlayer mIBinder;
	   
	   private ServiceConnection mServiceConnection = new ServiceConnection() {

		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			 mIBinder = IMediaPlayer.Stub.asInterface(service);
			   Log.i("client", "onServiceConnected");
		}

		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub 
			   Log.i("client", "onServiceDisconnected");
		}
		  
	   };   
	   
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mPlayBtn = (Button)findViewById(R.id.play);
        mStopBtn = (Button)findViewById(R.id.stop);
        mExitBtn = (Button)findViewById(R.id.exit);
        mShowText = (TextView)findViewById(R.id.show);
        
        mPlayBtn.setOnClickListener(this);
        mStopBtn.setOnClickListener(this);
        mExitBtn.setOnClickListener(this);
        
       
        Intent intent = new Intent("com.eric.ipc.aidl");
        //startService(intent);
        bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
        mShowText.setText("aidl achieve");
    }

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId())
		{
			case R.id.play:
			try 
			{
				Double result = mIBinder.add(1.0, 2.0);
				mShowText.setText(Double.toString(result));
			} catch (Exception e) 
			{
				e.printStackTrace();
			}
			break;
		}
	}
    
    
}